//
//  NSDate+SF.m
//  SuperFormatter
//
//  Created by xxx on 14-6-21.
//  Copyright (c) 2014年 wtf. All rights reserved.
//

#import "NSDate+SF.h"

@implementation NSDate (SF)

- (NSString *)dateStringWithFormat: (SFDateFormats)format
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    NSDateComponents *components = [calendar components:
                                    NSSecondCalendarUnit | NSMinuteCalendarUnit |NSHourCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:self];
    
    NSString *dateString = @"";
    NSString *yearString = [NSString stringWithFormat:@"%04d-", components.year];
    NSString *monthString = [NSString stringWithFormat:@"%02d", components.month];
    NSString *dayString = [NSString stringWithFormat:@"%02d", components.day];
    NSString *hourString = [NSString stringWithFormat:@"%02d:", components.hour];
    NSString *minString = [NSString stringWithFormat:@"%02d", components.minute];
    NSString *secString = [NSString stringWithFormat:@":%02d", components.second];
    
    if (format & SFDateFormatBitYear) {
        dateString = [dateString stringByAppendingString:yearString];
    }
    if (format & SFDateFormatBitMonth) {
        dateString = [dateString stringByAppendingString:monthString];
    }
    
    if (format > SFDateFormatYM) {
        dateString = [dateString stringByAppendingString:@"-"];
    }
    
    BOOL isFormatContainsDay = NO;
    if (format & SFDateFormatBitDay) {
        isFormatContainsDay = YES;
        dateString = [dateString stringByAppendingString:dayString];
    }
    
    if (format & SFDateFormatBitHour) {
        if (isFormatContainsDay) {
            dateString = [dateString stringByAppendingString:@" "];
        }
        dateString = [dateString stringByAppendingString:hourString];
    }
    if (format & SFDateFormatBitMin) {
        dateString = [dateString stringByAppendingString:minString];
    }
    if (format & SFDateFormatBitSec) {
        dateString = [dateString stringByAppendingString:secString];
    }
    
    return dateString;
}

- (NSDate *)dateWithIncDays:(NSInteger)days;
{
    if ((days > 0 && days * SFSecondsPerDay < 0) ||
        (days < 0 && days * SFSecondsPerDay > 0)) {
        return nil;
    }
    return [self dateByAddingTimeInterval:days * SFSecondsPerDay];
}

- (NSDate *)zeroOfDate
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSUIntegerMax fromDate:self];
    components.hour = 0;
    components.minute = 0;
    components.second = 0;
    
    // components.nanosecond = 0 not available in iOS
    NSTimeInterval ts = (double)(int)[[calendar dateFromComponents:components] timeIntervalSince1970];
    return [NSDate dateWithTimeIntervalSince1970:ts];
}

- (NSDateComponents *)agedForNow
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSUIntegerMax fromDate:self];
    
    NSDateComponents *componentsForNow = [calendar components:NSUIntegerMax fromDate:[NSDate date]];
    
    NSDateComponents *componentsDifference = [[NSDateComponents alloc] init];
    componentsDifference.year = componentsForNow.year - components.year;
    componentsDifference.month = componentsForNow.month - components.month;
    
    
    return componentsDifference;
}

- (NSDate *)firstDayOfPreviousMonth
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSUIntegerMax fromDate:self];
    
    components.month -= 1;
    components.day = 1;
    components.hour = 0;
    components.minute = 0;
    components.second = 0;
    
    // components.nanosecond = 0 not available in iOS
    NSTimeInterval ts = (double)(int)[[calendar dateFromComponents:components] timeIntervalSince1970];
    return [NSDate dateWithTimeIntervalSince1970:ts];
}

- (NSDate *)firstDayOfNextMonth
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSUIntegerMax fromDate:self];
    
    components.month += 1;
    components.day = 1;
    components.hour = 0;
    components.minute = 0;
    components.second = 0;
    
    // components.nanosecond = 0 not available in iOS
    NSTimeInterval ts = (double)(int)[[calendar dateFromComponents:components] timeIntervalSince1970];
    return [NSDate dateWithTimeIntervalSince1970:ts];
}

- (NSDate *)firstDayOfMonth
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSUIntegerMax fromDate:self];
    
    components.day = 1;
    components.hour = 0;
    components.minute = 0;
    components.second = 0;
    
    // components.nanosecond = 0 not available in iOS
    NSTimeInterval ts = (double)(int)[[calendar dateFromComponents:components] timeIntervalSince1970];
    return [NSDate dateWithTimeIntervalSince1970:ts];
    
}

- (NSDate *)lastDayOfMonth
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSUIntegerMax fromDate:self];
    
    components.month += 1;
    components.day = 0;
    components.hour = 0;
    components.minute = 0;
    components.second = 0;
    
    // components.nanosecond = 0 not available in iOS
    NSTimeInterval ts = (double)(int)[[calendar dateFromComponents:components] timeIntervalSince1970];
    return [NSDate dateWithTimeIntervalSince1970:ts];
}

@end
